/**
 * 
 */
package kr.co.insoft.auth.repository.organization.impl;

import kr.co.insoft.auth.mapper.organization.OrganizationMapper;
import kr.co.insoft.auth.model.organization.OrganizationEntityModel;
import kr.co.insoft.auth.repository.organization.OrganizationRepository;
import kr.co.insoft.framework.exceptions.ExceptionCode;
import kr.co.insoft.framework.exceptions.RepositoryException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.Assert;

/**
 * @author IN-SOFT Inc. GoodwillDD(sylee@in-soft.co.kr)
 * 
 */
@Repository
public class OrganizationRepositoryImpl implements OrganizationRepository {
	@Autowired
	OrganizationMapper organizationMapper;

	/*
	 * (non-Javadoc)
	 * 
	 * @see kr.co.insoft.auth.repository.organization.OrganizationRepository#
	 * createOrganization
	 * (kr.co.insoft.auth.model.organization.OrganizationEntityModel)
	 */
	@Override
	public OrganizationEntityModel createOrganization(
			OrganizationEntityModel organizationEntityModel)
			throws RepositoryException {
		try {
			Assert.notNull(organizationEntityModel);
			Assert.notNull(organizationEntityModel.getOrgName());
		} catch (IllegalArgumentException e) {
			throw new RepositoryException(ExceptionCode.ARGUMENT_ERROR);
		}

		try {
			organizationEntityModel.setOrgid(organizationMapper.getUuid());
			organizationMapper.createOrganization(organizationEntityModel);
			return organizationEntityModel;
		} catch (Exception e) {
			throw new RepositoryException(ExceptionCode.REPOSITORY_EXCEPTION, e);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see kr.co.insoft.auth.repository.organization.OrganizationRepository#
	 * updateOrganizationManager
	 * (kr.co.insoft.auth.model.organization.OrganizationEntityModel)
	 */
	@Override
	public void updateOrganizationManager(
			OrganizationEntityModel organizationEntityModel)
			throws RepositoryException {
		try {
			Assert.notNull(organizationEntityModel);
			Assert.notNull(organizationEntityModel.getOrgManager());
			Assert.notNull(organizationEntityModel.getOrgid());
		} catch (IllegalArgumentException e) {
			throw new RepositoryException(ExceptionCode.ARGUMENT_ERROR);
		}

		try {
			organizationMapper
					.updateOrganizationManager(organizationEntityModel);
		} catch (Exception e) {
			throw new RepositoryException(ExceptionCode.REPOSITORY_EXCEPTION, e);
		}
	}
}
